package belajarzk.action.master.helper;

import org.hibernate.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Center;
import org.zkoss.zul.South;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Window;

import belajarzk.common.Common;
import belajarzk.database.hibernate.HibernateUtil;
import belajarzk.database.model.Tbmuser;
import belajarzk.ui.util.MyTolbarbutton;


public class ChangePasswordWindow extends Window {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7041626862427552460L;

	private Textbox pl;
	private Textbox pb;
	private Textbox upb;

	public ChangePasswordWindow() {
		super();
		display();
	}

	public ChangePasswordWindow(String title, String border, boolean closable) {
		super(title, border, closable);
		display();
	}

	@SuppressWarnings("deprecation")
	private void display() {
		Common.clear(this);
		setPosition("center");
		setHeight("130px");
		setWidth("450px");
		setTitle("Ganti Password");
		Borderlayout borderlayout = new Borderlayout();
		borderlayout.setParent(this);

		Center north = new Center();
		north.setParent(borderlayout);

		Grid grid = new Grid();
		grid.setParent(north);
		grid.setWidth("100%");
		grid.setHeight("100%");
		

		Rows rows = new Rows();
		rows.setParent(grid);

		Row row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Password Lama"));
		row.appendChild(pl = new Textbox());
		pl.setCols(40);
		pl.setType("password");

		row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Password Baru"));
		row.appendChild(pb = new Textbox());
		pb.setCols(40);
		pb.setType("password");

		row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Ketik Ulang Password Baru"));
		row.appendChild(upb = new Textbox());
		upb.setCols(40);
		upb.setType("password");

		row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.setSpans("2");

		South south = new South();
		south.setParent(borderlayout);

		Toolbar hbox = new Toolbar();
		hbox.setHeight("40px");
		hbox.setParent(south);
		MyTolbarbutton cancel = new MyTolbarbutton("Cancel", "/img/cancel.gif");
		cancel.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				ChangePasswordWindow.this.setVisible(false);
			}
		});
		cancel.setParent(hbox);
		MyTolbarbutton save = new MyTolbarbutton("Save", "/img/save.gif");
		save.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				onChangePassword(null);
			}
		});
		save.setParent(hbox);

	}

	public void onChangePassword(Event event) throws Exception {
		Tbmuser users = (Tbmuser) Sessions.getCurrent().getAttribute("users");
		if (users == null) {
			users = (Tbmuser) Sessions.getCurrent().getAttribute("usersTemp");
		}

		if (pl.getValue().trim().equals("")) {
			Messagebox.show("Old password can not empty", "Warning", 1,
					Messagebox.INFORMATION);
			return;
		}
		if (pb.getValue().trim().equals("")) {
			Messagebox.show("New password can not empty", "Warning", 1,
					Messagebox.INFORMATION);
			return;
		}
		if (!pl.getValue().trim().equals(users.getUserPassword().trim())) {
			Messagebox.show("Old password incorect", "Warning", 1,
					Messagebox.INFORMATION);
			return;
		}
		if (!pb.getValue().trim().equals(upb.getValue().trim())) {
			Messagebox.show("New password not equal", "Warning", 1,
					Messagebox.INFORMATION);
			return;
		}
		users.setUserPassword((pb.getValue().trim()));
		Session ses = HibernateUtil.currentSession();

		ses.update(users);

		Sessions.getCurrent().setAttribute("users", users);
		Messagebox.show("Password changed successfully", "Warning", 1,
				Messagebox.INFORMATION);
		pl.setText("");
		pb.setText("");
		upb.setText("");
		ses.clear();

		this.setVisible(false);
	}

}
